var c = require('../lib/common.js');
var cf= require('../lib/config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['辽宁省', '沈阳市', '和平区'],
    customItem: '全部',
    addr:0,
    addurl:"/images/kong.png",
    edit:0,
    submit:'保存',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var address_id = options.address_id;
    var that = this;
    if (address_id) {
      this.setData({ edit: 1, submit: '编辑', address_id: address_id})
      c.ajax(
        cf.hostapi + 'toIdGetAddressInfo', {
          id: address_id
        },
        res => {
          if(res.data.data.is_def==1){
            that.setData({ addurl: "/images/shi.png", addr:1})
          };
          that.setData({ data: res.data.data, address: res.data.data.details, phone: res.data.data.phone, men: res.data.data.address, name: res.data.data.name, latitude: res.data.data.lat, longitude: res.data.data.lng})
        }
      )
    } else {
      var address_id = 0
      this.address();
    }
    // var user_id = wx.getStorageSync('userInfo').user_id;
    // this.setData({
    //   address_id: address_id,
    //   user_id: user_id
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  username:function(e){
   var name =e.detail.value;
    this.setData({ name: name})
  },
  phone:function(e){
    var phone = e.detail.value;
    this.setData({ phone: phone })
  },
  men:function(e){
    var men = e.detail.value;
    this.setData({ men: men })
  },
    address:function(){
      var that=this;
      //1、获取当前位置坐标
      wx.chooseLocation({
     
        success: function (res) {
          console.log(res);
          //2、根据坐标获取当前位置名称，显示在顶部:腾讯地图逆地址解析
          var address  = res.address
          var latitude = res.latitude
          var longitude= res.longitude
          var name     = res.name
          that.setData({
            latitude: latitude,
            longitude: longitude,
            address: address,
            addressname: name
          })
          // wx.request({
          //   url: 'https://www.cwlyz.com/wx/Index/address', //仅为示例，并非真实的接口地址
          //   data: {
          //     lat: latitude,
          //     lng: longitude
          //   },
          //   header: {
          //     'content-type': 'application/json' // 默认值
          //   },
          //   success: function (res) {
          //     that.setData({
          //       addressinfo: res.data.data.address,
          //       province: res.data.data.province,
          //       city: res.data.data.city,
          //       district: res.data.data.district,
          //     })

          //   }
          // })

        }
      })
    },
  submit:function(){
    var uid=wx.getStorageSync('userinfo').id;
    var name=this.data.name;
    var phone = this.data.phone;
      console.log(phone);
    if (name == undefined) {
      wx.showToast({
        title: '请填写姓名',
        image: '/images/closered.png',
      })
      return;
    }
      var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
      if (!phoneReg.test(phone)) {
        wx.showToast({
          title: '手机号码有误',
          image: '/images/closered.png',
        })
        return;
      }
    var details = this.data.address;
    var address = this.data.men;
    var lat = this.data.latitude;
    var lng = this.data.longitude;
    var is_def = this.data.addr;
    var edit = this.data.edit;
    var address_id = this.data.address_id;

    if (edit){
      c.ajax(
        cf.hostapi + 'editAddress',
        { uid: uid, name: name, phone: phone, details: details, address: address, lat: lat, lng: lng, is_def: is_def, id: address_id },
        res => {
          if (res.data.retcode == 200) {
            wx.showToast({
              title: '修改成功!',
              image: '/images/dui.png'
            })
            setTimeout(function () {
              wx.navigateTo({
                url: '/pages/address/address?page=4',
              })
            }, 1000)

          }else{
            wx.showToast({
              title: '修改失败！',
              image: '/images/closered.png',
            })
          };
        }
      )
    }else{
      c.ajax(
        cf.hostapi + 'addAddress',
        { uid: uid, name: name, phone: phone, details: details, address: address, lat: lat, lng: lng, is_def: is_def },
        res => {
          if (res.data.retcode == 200) {
            wx.showToast({
              title: '添加成功!',
              image: '/images/dui.png'
            })
            setTimeout(function () {
              wx.navigateTo({
                url: '/pages/address/address?page=4',
                success: function (res) { },
                fail: function (res) { },
                complete: function (res) { },
              })
            }, 1000)

          };
        }
      )
    }
    
  },
  addressmoren:function(e){
    var addr=e.currentTarget.dataset.addr;
    
    if(addr==0){
      
      this.setData({ addurl: '/images/shi.png', addr:1})
    }else{
      this.setData({ addurl: '/images/kong.png', addr: 0 })
    }
  }

})